const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    page_size:20,
    items:'',
    kong:false
    // domain:Api.domain
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  goadd(){//去新增地址
    wx.navigateTo({
      url: '/pages/newAddress/newAddress',
    })
  },
  edit(e){//去修改地址
    let {index} = e.currentTarget.dataset
    app.address = this.data.items[index]
    wx.navigateTo({
      url: '/pages/editAddress/editAddress',
    })
  },
  goxuan(e){
    let {index} = e.currentTarget.dataset
    wx.$cache.set('dz',this.data.items[index])
    wx.navigateBack({
      delta: 1,
    })
  },
  
  init(){//收货地址列表
    var that = this
    var params = {
      page:1,
      page_size:this.data.page_size
    }
    wx.$http.get(wx.$get.list_goods_address,params).then(res=>{
      console.log('收货地址列表', res)
      if(res.length==0){
        this.setData({
          kong:true
        })
      }else{
        this.setData({
            kong:false
        })
      }
      that.setData({
        items:res
      })
    })
  },
  shemo(e){//把地址设为默认
    let {id} = e.currentTarget.dataset
    var that = this
    var params = {
      goods_address_id:id
    }
    wx.$http.get(wx.$get.goods_address_default,params).then(res=>{
      console.log('把地址设为默认', res)
      that.init()
    })
  },
  pulldown(){//收货地址列表上拉加载
    var that = this
    var params = {
      page:1,
      page_size:this.data.page_size
    }
    wx.$http.get(wx.$get.list_goods_address,params).then(res=>{
      console.log('收货地址列表', res)
      if(res.length!=0){
        that.setData({
          items:[...that.data.items,...res]
        })
      }
    })
  },
  del(e){//删除
    let {id} = e.currentTarget.dataset
    var that = this
    wx.showModal({
      title: '提示',
      content: '你确定要删除吗',
      success (res) {
        if (res.confirm) {
          var params = {
            goods_address_id:id
          }
          wx.$http.get(wx.$get.del_goods_address,params).then(res=>{
            console.log('删除', res)
            that.init()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.pulldown()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})